﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;

namespace WP.RM.Form
{
    public partial class frmRMProductBOMRevSetting : BaseQryRpt 
    {
        bool blnPressExit = true ;
        string mstrFromType = "REV";
        public frmRMProductBOMRevSetting(string strFromType)
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));           
            this.MinimumSize = this.Size;
            mstrFromType = strFromType;
        }


        public string GetCondition()
        {
            this.ShowDialog();
            if (blnPressExit == true ) return "";
            string strCond = " where 1=1 ";
            strCond += " and productname>='" + txtStProductName.Text + "' and productname<='" + txtEdProductName.Text + "'";
            strCond += " and creator >='"+ txtStCreator.Text +"' and creator<='"+ txtEdCreator.Text +"'";
            strCond +=" and CategoryName>='"+ txtStCategoryName.Text  +"' and CategoryName<='"+ txtEdCategoryName.Text  +"'";
            strCond +=" and SetName>='"+ txtStSetName.Text  +"' and SetName<='"+ txtEdSetName.Text  +"'";
            strCond +=" and ProductName in ";
            strCond += " (  ( select ProductName from "+ ( mstrFromType =="isRev" ? "rmProductBOMLabourCostRev":"rmProductBOMLabourCostSales") +" where ItemNo>='" + txtStItemNo.Text + "' and ItemNo<='" + txtEdItemNo.Text + "' )";
            strCond +=" union ( select ProductName from "+ ( mstrFromType =="isRev" ? "rmProductBOMDetailRev":"rmProductBOMDetailSales") +" where ItemNo>='" + txtStItemNo.Text + "' and ItemNo<='" + txtEdItemNo.Text + "' ) )";
            if (BaseFunctions.IsDate(datStCreateDate.Value))
            {
                strCond += " and CreateDate>='" + datStCreateDate.Value + "'";
            }
            if (BaseFunctions.IsDate(datEdCreateDate.Value))
            {
                strCond += " and CreateDate<='"+ datEdCreateDate.Value +"'";
            }
            if (cmbVerClosed.SelectedIndex == 0)
            {
                strCond += " and VerClosed=0";
            }
            else if ( cmbVerClosed.SelectedIndex ==1)
            {
                strCond += " and VerClosed=1";
            }            
            
            return strCond;
        }

        private void initControl()
        {
            DBAccess dbcon = new DBAccess ( false ,false );
            string strMaxValue = "", strMinValue = "";
            DataFunctions.GetMaxMinRec(dbcon, "rmProduct", "ProductName", ref strMaxValue, ref strMinValue);
            txtStProductName.Text = strMinValue;
            txtEdProductName.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "mUser", "UserName", ref strMaxValue, ref strMinValue);
            txtStCreator.Text = strMinValue;
            txtEdCreator.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "rmCategory", "CategoryName", ref strMaxValue, ref strMinValue);
            txtStCategoryName.Text = strMinValue;
            txtEdCategoryName.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "rmSet", "SetName", ref strMaxValue, ref strMinValue);
            txtStSetName.Text = strMinValue;
            txtEdSetName.Text = strMaxValue;

            DataFunctions.GetMaxMinRec(dbcon, "ImInvMaster", "ItemNo", ref strMaxValue, ref strMinValue);
            txtStItemNo.Text = strMinValue;
            txtEdItemNo.Text = strMaxValue;

            datEdCreateDate.Value = BaseFunctions.FormatDate(DateTime.Now.Date.ToShortDateString (), DateFormatOption.DateFmtMDY);  
            
        }

        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strFindValue = string.Empty;
            string strFieldName = string.Empty;
            string strSql = string.Empty;
            string strCaption = string.Empty;
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;
            switch (con.Name)
            {
                case "ubtStProductName":
                case "ubtEdProductName":
                    strFindValue = (con.Name == "ubtStProductName" ? txtStProductName.Text : txtEdProductName.Text);
                    strFieldName = "ProductName";
                    strSql = BaseFunctions.GetLookUpSQL("rmProduct");
                    ocon = (con.Name == "ubtStProductName" ? txtStProductName : txtEdProductName);
                    break;
                case "ubtStCreator":
                case "ubtEdCreator":
                    strFindValue = (con.Name == "ubtStCreator" ? txtStCreator.Text : txtEdCreator.Text);
                    strFieldName = "UserName";
                    strSql = BaseFunctions.GetLookUpSQL("mUser");
                    ocon = (con.Name == "ubtStCreator" ? txtStCreator : txtEdCreator);
                    break;
                case "ubtStCategoryName" :
                case "ubtEdCategoryName" :
                    strFindValue = (con.Name == "ubtStCategoryName" ? txtStCategoryName.Text : txtEdCategoryName.Text);
                    strFieldName = "CategoryName";
                    strSql = BaseFunctions.GetLookUpSQL("rmCategory");
                    ocon = (con.Name == "ubtStCategoryName" ? txtStCategoryName : txtEdCategoryName);
                    break;
                case "ubtStSetName" :
                case "ubtEdSetName" :
                    strFindValue = (con.Name == "ubtStSetName" ? txtStSetName.Text : txtEdSetName.Text);
                    strFieldName = "SetName";
                    strSql = BaseFunctions.GetLookUpSQL("rmSet");
                    ocon = (con.Name == "ubtStSetName" ? txtStSetName : txtEdSetName);
                    break;
                case "ubtStItemNo":
                case "ubtEdItemNo":
                    strFindValue = (con.Name == "ubtStItemNo" ? txtStItemNo.Text : txtEdItemNo.Text);
                    strFieldName = "ItemNo";
                    strSql = BaseFunctions.GetLookUpSQL("ImInvMaster");
                    ocon = (con.Name == "ubtStItemNo" ? txtStItemNo : txtEdItemNo);
                    break;
            }
            if (mblnLookUpSortByDescription) strSql = DataFunctions.GetSqlByDescriptionSort(strSql);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(strSql, strCaption, strFindValue, strFieldName);
            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                ocon.Text = frmlookup.GetReturnValue(strFieldName);
                ocon.Focus();
            }
            frmlookup.Close();

        }

        private void frmRMProductBOMRevSetting_Load(object sender, EventArgs e)
        {
            initControl();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            blnPressExit = true;
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            blnPressExit = false;
            this.Close();
        }

        private void txtStItemNo_TextChanged(object sender, EventArgs e)
        {
            Control con = sender as Control;
          
            
            if (con == null) return;
            if (con.Name == txtStItemNo.Name)
            {
                lblStItemName.Text = "";
                lblStItemName.Text = DataFunctions.GetFieldValue("iminvmaster", "itemname", "itemno='" + con.Text + "'");
            }
            else
            {
                lblEdItemName.Text = "";
                lblEdItemName.Text = DataFunctions.GetFieldValue("iminvmaster", "itemname", "itemno='" + con.Text + "'");
            }
        }

    }
}
